package cn.tqfeiyang.smartadmin.office.controller;

import cn.tqfeiyang.smartadmin.office.service.NoticeCategoryService;
import cn.tqfeiyang.smartadmin.office.request.NoticeCategoryCreateDTO;
import cn.tqfeiyang.smartadmin.office.request.NoticeCategoryUpdateDTO;
import cn.tqfeiyang.smartadmin.office.request.NoticeCategoryQueryDTO;
import cn.tqfeiyang.smartadmin.office.response.NoticeCategoryQueryVO;
import cn.tqfeiyang.smartadmin.office.response.NoticeCategoryDetailVO;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.smartadmin.commons.pagination.PageData;
import cn.tqfeiyang.smartadmin.commons.response.ApiResult;
import cn.tqfeiyang.smartadmin.commons.validation.EntityId;
import cn.tqfeiyang.smartadmin.commons.logging.OperationLog;

import java.util.Set;

/**
 * 通知分类控制器
 *
 * @author tqfeiyang
 * @since 2024-09-07
 */
@RestController
@Validated
@RequestMapping("/api/office/noticeCategory")
@Tag(name = "办公/通知分类")
public class NoticeCategoryController {

    /**
     * 通知分类服务
     */
    @Resource
    private NoticeCategoryService noticeCategoryService;

    /**
     * 分页查询通知分类
     *
     * @param dto 请求数据
     * @return 通知分类分页
     */
    @OperationLog
    @GetMapping("/getNoticeCategoryPage")
    @Operation(summary = "通知分类 - 分页查询")
    @SaCheckPermission("office:noticeCategory:getNoticeCategoryPage")
    public ApiResult<PageData<NoticeCategoryQueryVO>> getNoticeCategoryPage(@Validated NoticeCategoryQueryDTO dto) {
        return ApiResult.success(noticeCategoryService.getNoticeCategoryPage(dto));
    }

    /**
     * 获取全部通知分类
     *
     * @return 通知分类列表
     */
    @OperationLog
    @GetMapping("/getAllNoticeCategoryList")
    @Operation(summary = "通知分类 - 获取全部")
    @SaCheckPermission("office:noticeCategory:getAllNoticeCategoryList")
    public ApiResult<NoticeCategoryQueryVO> getAllNoticeCategoryList() {
        return ApiResult.success(noticeCategoryService.getAllNoticeCategoryList());
    }

    /**
     * 获取通知分类详情
     *
     * @param categoryId 分类id
     * @return 通知分类详情
     */
    @OperationLog
    @GetMapping("/getNoticeCategoryInfo/{categoryId}")
    @Operation(summary = "通知分类 - 获取详情")
    @SaCheckPermission("office:noticeCategory:getNoticeCategoryInfo")
    public ApiResult<NoticeCategoryDetailVO> getNoticeCategoryInfo(@PathVariable @EntityId Long categoryId) {
        return ApiResult.success(noticeCategoryService.getNoticeCategoryInfo(categoryId));
    }

    /**
     * 添加通知分类
     *
     * @param dto 请求数据
     * @return 通知分类详情
     */
    @OperationLog
    @PostMapping("/createNoticeCategory")
    @Operation(summary = "通知分类 - 添加")
    @SaCheckPermission("office:noticeCategory:createNoticeCategory")
    public ApiResult<NoticeCategoryDetailVO> createNoticeCategory(@RequestBody @Validated NoticeCategoryCreateDTO dto) {
        Long categoryId = noticeCategoryService.createNoticeCategory(dto);
        return ApiResult.success(noticeCategoryService.getNoticeCategoryInfo(categoryId));
    }

    /**
     * 修改通知分类
     *
     * @param categoryId 分类id
     * @param dto 请求数据
     * @return 通知分类详情
     */
    @OperationLog
    @PostMapping("/updateNoticeCategory/{categoryId}")
    @Operation(summary = "通知分类 - 修改")
    @SaCheckPermission("office:noticeCategory:updateNoticeCategory")
    public ApiResult<NoticeCategoryDetailVO> updateNoticeCategory(@PathVariable @EntityId Long categoryId, @RequestBody @Validated NoticeCategoryUpdateDTO dto) {
        noticeCategoryService.updateNoticeCategory(categoryId, dto);
        return ApiResult.success(noticeCategoryService.getNoticeCategoryInfo(categoryId));
    }

    /**
     * 删除通知分类
     *
     * @param categoryId 分类id
     */
    @OperationLog
    @PostMapping("/deleteNoticeCategory/{categoryId}")
    @Operation(summary = "通知分类 - 删除")
    @SaCheckPermission("office:noticeCategory:deleteNoticeCategory")
    public ApiResult deleteNoticeCategory(@PathVariable @EntityId Long categoryId) {
        noticeCategoryService.deleteNoticeCategory(categoryId);
        return ApiResult.success();
    }

    /**
     * 批量删除通知分类
     *
     * @param categoryIds 分类id集合
     */
    @OperationLog
    @PostMapping("/batchDeleteNoticeCategories")
    @Operation(summary = "通知分类 - 批量删除")
    @SaCheckPermission("office:noticeCategory:batchDeleteNoticeCategories")
    public ApiResult batchDeleteNoticeCategories(@RequestBody Set<Long> categoryIds) {
        noticeCategoryService.batchDeleteNoticeCategories(categoryIds);
        return ApiResult.success();
    }

    /**
     * 导出Excel
     *
     * @param response 响应对象
     */
    @OperationLog
    @GetMapping("/exportNoticeCategoryExcel")
    @Operation(summary = "通知分类 - 导出Excel")
    @SaCheckPermission("office:noticeCategory:exportNoticeCategoryExcel")
    public void exportNoticeCategoryExcel(HttpServletResponse response) {
        noticeCategoryService.exportNoticeCategoryExcel(response);
    }

    /**
     * 修改排序
     *
     * @param categoryId 分类id
     * @param sort 排序
     */
    @OperationLog
    @PostMapping("/updateSortField/{categoryId}/{sort}")
    @Operation(summary = "通知分类 - 修改排序")
    @SaCheckPermission("office:noticeCategory:updateSortField")
    public ApiResult updateSortField(@PathVariable @EntityId Long categoryId, @PathVariable Integer sort) {
        noticeCategoryService.updateSortField(categoryId, sort);
        return ApiResult.success();
    }
}
